﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SysSurv.dtl;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;

namespace SysSurv.bll
{
    class Wiii
    {
        #region Attributes
        private User usrLog;
        public Survey srvAux { get; set; }
        public UserSurvey us { get; set; }
        #endregion
        #region Singleton
        private static Wiii wiiInstance;
        private Wiii() {
            usrLog = null;
        }
        public static Wiii getInstance() {
            if (wiiInstance == null) {
                wiiInstance = new Wiii();
            }
            return wiiInstance;
        }
        #endregion
        #region Properties
        public User UsrLog
        {
            get { return usrLog; }
            set { usrLog = value; }
        }
        #endregion
        public bool login(string pNick, string pPass) {
            bool aux = false;
            User usr = new User(pNick, pPass);
            string json = new JavaScriptSerializer().Serialize(usr);
            RestClient client = new RestClient("http://localhost:8080/SpringSurv/users/login", HttpVerb.POST, json, true);
            string response = client.MakeRequest();
            aux = new JavaScriptSerializer().Deserialize<bool>(response);
            if (aux == true) {
                usrLog = usr;
            }
            return aux;
        }
        public void createUser(string pNick, string pPass) {
            User usr = new User(pNick, pPass);
            string json = new JavaScriptSerializer().Serialize(usr);
            RestClient client = new RestClient("http://localhost:8080/SpringSurv/users", HttpVerb.POST,json , false);
            client.MakeRequest();
        }
        public List<Survey> getUserSurveys() { 
            List<Survey> lstAux = new List<Survey>();
            string json = new JavaScriptSerializer().Serialize(usrLog);
            RestClient client = new RestClient("http://localhost:8080/SpringSurv/surveys/creator", HttpVerb.POST, json, true);
            string response = client.MakeRequest();
            lstAux = new JavaScriptSerializer().Deserialize<List<Survey>>(response);
            return lstAux;
        }
        public List<Survey> getAvSurveys() {
            List<Survey> lstAux = new List<Survey>();
            string json = new JavaScriptSerializer().Serialize(usrLog);
            RestClient client = new RestClient("http://localhost:8080/SpringSurv/surveys/notFilled", HttpVerb.POST, json, true);
            string response = client.MakeRequest();
            lstAux = new JavaScriptSerializer().Deserialize<List<Survey>>(response);
            return lstAux;
        }
        public void createSurvey(Survey pSurvey) {
            string json = new JavaScriptSerializer().Serialize(pSurvey);
            RestClient client = new RestClient("http://localhost:8080/SpringSurv//surveys/new", HttpVerb.POST, json, false);
            client.MakeRequest();
        }
    }
}
